#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int n, k;
int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> k;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		if (n == 1) {
			cout << 0 << endl;
			continue;
		}
		sort(a + 1, a + n + 1);
		int ans = 0;
		int res = 0;
		for (int i = 2; i <= n; i++) {
			if (a[i] - a[i - 1] <= k) {
				ans++;
			}
			else {
				ans = 0;
			}
			res = max(res, ans);
		}
		cout << n - res - 1 << endl;
	}
	return 0;
}